package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.baidumap.DistanceUtil;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.pojo.domain.Shop;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgDto;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg, SearchMasterMsgQuery> implements ISearchMasterMsgService {


    @Autowired
    private SearchMasterMsgMapper msgMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper opaMapper;

    @Override
    public PageList<SearchMasterMsg> findPage(SearchMasterMsgQuery query) {

        //2.查询所有  12条
        List<SearchMasterMsg> searchMasterMsgs = msgMapper.selectAll();
        if (CollectionUtil.isEmpty(searchMasterMsgs)){

            return new PageList<>();
        }

        //获取店铺地址  店铺对象  店铺id?
        Long shopId = LoginInfoUtil.getShopId();
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        String shopAddress = shop.getAddress();

        //7条
        List<SearchMasterMsg> msgList = searchMasterMsgs.stream()
                //以什么条件过滤  item：list里面的每条数据
                .filter(item -> DistanceUtil.isInCircle(shopAddress, item.getAddress(), 5000))
                //收集成list
                .collect(Collectors.toList());

        //用了分页底层是个page类型，只有page类型才能pageInfo
        System.out.println(searchMasterMsgs.getClass());

        //分页 limit  （page -1 ）* size ，size

        //lambda :（page -1 ）* size ，size   7条
        List<SearchMasterMsg> msgs = msgList.stream()
                //skip：跳过的意思 1页  0  2页： 5
                .skip((query.getPage() - 1) * query.getSize())
                //告诉这一页只要前5个
                .limit(query.getSize()).collect(Collectors.toList());

        //返回分页结果
        return new PageList<>((long)msgList.size(),msgs);
    }

    @Override
    @Transactional
    public void jieDan(SearchMasterMsgDto msgDto) {

        //接单  验证

        //0.查询订单表的数据                                              接单表id
        SearchMasterMsg masterMsg = msgMapper.selectByPrimaryKey(msgDto.getId());

        //1.修改接单表的state 和 shop_id
        masterMsg.setState(1);//接单
        masterMsg.setShopId(LoginInfoUtil.getShopId());

        msgMapper.updateByPrimaryKeySelective(masterMsg);

        //2.数据封装成收购订单数据
        OrderPetAcquisition opa = BeanUtil.copyProperties(masterMsg, OrderPetAcquisition.class);
        opa.setId(null);
        opa.setDigest(msgDto.getNote());
        opa.setState(0);//0:待支付
        String orderSn = new Snowflake().nextIdStr();
        opa.setOrderSn(orderSn);
        opa.setPayType(0);//垫付
        opa.setEmpId(msgDto.getEmpId());

        //3.入库
        opaMapper.insertSelective(opa);


    }

    public static void main(String[] args) {

        long l = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {

            new Snowflake().nextIdStr();
        }
        long l2 = System.currentTimeMillis();
        System.out.println(l2-l);
    }
}
